package com.workstudy.controller;

import com.github.pagehelper.PageInfo;
import com.workstudy.domain.User;
import com.workstudy.service.UserService;
import com.workstudy.support.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;
import zhibi.common.util.security.SecurityUtil;

import java.util.Objects;


@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 个人信息
     *
     * @param model
     * @return
     */
    @RequestMapping("info")
    public String info(Model model) {
        User user = userService.selectByPK(sessionUser().getId());
        model.addAttribute(user);
        return "user/detail";
    }

    /**
     * 用户详情
     *
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        User user = userService.selectByPK(id);
        if (null == user) throw new MessageException("该用户不存在");
        model.addAttribute(user);
        return "user/detail";
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @RequestMapping("update")
    public String update(User user) {
        User temp = new User();
        temp.setNo(user.getNo());
        temp = userService.selectOne(temp);
        if (null != temp && !Objects.equals(temp.getId(), user.getId())) {
            throw new MessageException("该学号已经存在，请换一个学号");
        }
        temp = new User();
        temp.setPhone(user.getPhone());
        temp = userService.selectOne(temp);
        if (null != temp && !Objects.equals(temp.getId(), user.getId())) {
            throw new MessageException("该手机号已经存在，请换一个手机号");
        }
        temp = new User();
        temp.setIdcard(user.getIdcard());
        temp = userService.selectOne(temp);
        if (null != temp && !Objects.equals(temp.getId(), user.getId())) {
            throw new MessageException("该身份证号已经存在，请换一个身份证号");
        }
        userService.updateByIdSelective(user);
        return refresh();
    }

    /**
     * 用户列表
     *
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(User user, Page page, Model model) {
        Example example = ExampleUtil.getInstance()
                .addParam("type", "student")
                .addParam("no", user.getNo(), ExampleType.Operation.LIKE)
                .addParam("name", user.getName(), ExampleType.Operation.LIKE)
                .getExample();
        PageInfo<User> userPageInfo = userService.selectByExample(example, page);
        setModelAttribute(model, userPageInfo);
        return "user/list";
    }

    @RequestMapping("add")
    public String add() {
        return "user/add";
    }

    @RequestMapping("save")
    public String save(User user) {
        User temp = new User();
        temp.setNo(user.getNo());
        temp = userService.selectOne(temp);
        if (null != temp) {
            throw new MessageException("该学号已经存在，请换一个学号");
        }
        temp = new User();
        temp.setPhone(user.getPhone());
        temp = userService.selectOne(temp);
        if (null != temp) {
            throw new MessageException("该手机号已经存在，请换一个手机号");
        }
        temp = new User();
        temp.setIdcard(user.getIdcard());
        temp = userService.selectOne(temp);
        if (null != temp) {
            throw new MessageException("该身份证号已经存在，请换一个身份证号");
        }
        String password = user.getIdcard();
        if (password.length() > 5) {
            password = password.substring(password.length() - 5, password.length());
        }
        user.setPassword(SecurityUtil.MD5(password, 1));
        user.setType("student");
        userService.insertSelective(user);
        return "redirect:/user/list";
    }

    /**
     * 删除学生
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id){
        userService.deleteByPK(id);
        return "redirect:/user/list";
    }




}
